package com.android.clinic.app.ui.doctor;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.android.clinic.app.ClinicApplication;
import com.android.clinic.app.R;
import com.android.clinic.app.base.BaseActivity;
import com.android.clinic.app.entity.AppointInfo;
import com.android.clinic.app.entity.DoctorInfo;
import com.android.clinic.app.http.HttpClient;
import com.android.clinic.app.http.base.BaseObserver;
import com.android.clinic.app.http.utils.RxObservableUtils;
import com.android.clinic.app.ui.widget.UIAlertView;
import com.android.clinic.app.ui.widget.calendar.CalendarView;
import com.android.clinic.app.utils.Constants;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by cheng on 2018/2/7.
 * 坐诊信息(线下面诊)
 */
public class SitDiagnosedActivity extends BaseActivity {

    private CalendarView mCalendarView;
    private TextView mTxtDate;

    private DoctorInfo mDoctorInfo;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_sit_diagnosed;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        setTitleBarTitle("坐诊信息");
        setTitleBarLeftResource(R.drawable.icon_back);

        mDoctorInfo = (DoctorInfo) this.getIntent().getSerializableExtra("doctorInfo");

        mTxtDate = (TextView) findViewById(R.id.txt_date);
        mCalendarView = (CalendarView) findViewById(R.id.calendarView);
        // 设置是否能够改变日期状态
        mCalendarView.setChangeDateStatus(false);
        // 设置日期点击监听
        mCalendarView.setOnDataClickListener((view, date) -> UIAlertView.alertDialog(SitDiagnosedActivity.this,
                "操作提示", "是否预约?", new UIAlertView.OnThemeDialogListener() {
            @Override
            public void OnDismissButton() {

            }

            @Override
            public void OnSureButton() {
                postAppointing(date);
            }
        }, "确定", "取消"));
        setCurDate();
    }

    /**
     * 选择预约时间
     * @param time
     */
    private void postAppointing(String time) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("appointTime", time);
            jsonObject.put("doctorId", mDoctorInfo.getId());
            jsonObject.put("userId", ClinicApplication.getUserInfo().getId());
            jsonObject.put("deptId", mDoctorInfo.getDeptId());
            jsonObject.put("deptName", mDoctorInfo.getDeptName());
            jsonObject.put("hospitalId", mDoctorInfo.getHospitalId());
            jsonObject.put("hospitalName", mDoctorInfo.getHospitalName());

            MediaType jsonType = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(jsonType, jsonObject.toString());
            HttpClient.getHttpService().postAppoint(Constants.APPOINT_URL, body).compose(this.bindToLifecycle())
                    .compose(RxObservableUtils.applySchedulers())
                    .subscribe(new BaseObserver<AppointInfo>() {
                        @Override
                        public void onSuccess(AppointInfo appointInfo) {
                            Intent intent = new Intent(SitDiagnosedActivity.this, BuyServiceActivity.class);
                            intent.putExtra("doctorInfo", mDoctorInfo);
                            intent.putExtra("appointId", appointInfo.getId());
                            intent.putExtra("inquiryType", "2");
                            startActivity(intent);
                        }

                        @Override
                        public void onFailure(String message) {
                            showToast(message);
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void setCurDate(){
        mTxtDate.setText(mCalendarView.getYear() + "年" + (mCalendarView.getMonth() + 1) + "月");
    }
}
